<template>
  <div class="app-container">
    <div>
      <el-row :gutter="20">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="任务名称" prop="taskName">
            <el-input
              v-model="queryParams.taskName"
              placeholder="请输入任务名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="工单名称" prop="workOrderName">
            <el-input
              v-model="queryParams.workOrderName"
              placeholder="请输入工单名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="工单状态" prop="workOrderStauts">
            <el-select v-model="queryParams.workOrderStauts" placeholder="请选择工单状态">
              <el-option
                v-for="dict in workOrderStautsS"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
    </div>
    <el-tabs v-model="activeName" >
      <el-tab-pane label="加时间列表" name="first" style="height: 600px;">
<!--          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <span>加时间列表</span>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="znddListTime"></right-toolbar>
          </el-row>-->

          <el-table v-loading="loading" :data="znddListTime" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
            <el-table-column label="任务名称" align="center" width="180" prop="taskName" />
            <el-table-column label="工单名称" align="center" width="180" prop="workOrderName" show-overflow-tooltip/>
            <el-table-column label="工单号" align="center" width="180" prop="workOrderNo" show-overflow-tooltip/>
            <el-table-column label="工单状态" align="center" width="150" prop="workOrderStauts" :formatter="solutionFormatter"/>
            <el-table-column label="生产总车序" align="center" width="130" prop="ji" />
            <el-table-column label="任务车序" align="center" width="130" prop="carOrder" />
            <el-table-column label="工序顺序" align="center" width="130" prop="orderSequence" />
            <el-table-column label="解决方案" align="center" width="550" prop="solutionDescribe" :formatter="formatTime" show-overflow-tooltip/>
            <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
          </el-table>

          <pagination v-show="totalTime > 0" :total="totalTime" :page.sync="queryParamsTime.pageNum" :limit.sync="queryParamsTime.pageSize"
                      @pagination="getTimeList" />
      </el-tab-pane>
      <el-tab-pane label="加人列表" name="second" style="height: 600px;">
          <el-table
            ref="peopleTable"
            v-loading="loading"
            :data="znddListPeople"
            @selection-change="handleSelectionChange"
            >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
            <el-table-column label="任务名称" align="center" width="180" prop="taskName" />
            <el-table-column label="工单名称" align="center" width="180" prop="workOrderName" show-overflow-tooltip/>
            <el-table-column label="工单号" align="center" width="180" prop="workOrderNo" show-overflow-tooltip/>
            <el-table-column label="工单状态" align="center" width="150" prop="workOrderStauts" :formatter="solutionFormatter"/>
            <el-table-column label="生产总车序" align="center" width="130" prop="ji" />
            <el-table-column label="任务车序" align="center" width="130" prop="carOrder" />
            <el-table-column label="工序顺序" align="center" width="130" prop="orderSequence" />
            <el-table-column label="解决方案" align="center" width="550" prop="solutionDescribe" :formatter="formatTime" show-overflow-tooltip/>
            <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
          </el-table>

          <pagination v-show="totalPeople > 0" :total="totalPeople" :page.sync="queryParamsPeople.pageNum" :limit.sync="queryParamsPeople.pageSize"
                      @pagination="getPeopleList" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { listZndd,listTimeZndd,listPeopleZndd,getZndd, delZndd, addZndd, updateZndd } from "@/api/zndd/solution/solution";

export default {
  name: "solution",
  data() {
    return {
      activeName: 'first',
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //问题类型数据
      workOrderStautsS:[],
      // 解决方案信息表格数据
      znddList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        workOrderName: null,
        workOrderNo: null,
        workOrderStauts: null,
        carOrder: null,
      },
      queryParamsTime: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        workOrderName: null,
        workOrderNo: null,
        workOrderStauts: null,
        carOrder: null,
      },
      queryParamsPeople: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        workOrderName: null,
        workOrderNo: null,
        workOrderStauts: null,
        carOrder: null,
      },
      znddListTime: [],
      znddListPeople: [],
      totalTime: 0,
      totalPeople: 0,
      loadingTime: false,
      loadingPeople: false,
      // 记录滚动位置
      scrollPosition: 0,
      peopleTableScrollTop: 0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ],
        taskId: [
          { required: true, message: "任务id不能为空", trigger: "blur" }
        ],
        workOrderId: [
          { required: true, message: "工单id不能为空", trigger: "blur" }
        ],
        carOrder: [
          { required: true, message: "车辆顺序不能为空", trigger: "blur" }
        ],
        solutionDescribe: [
          { required: true, message: "解决方案说明不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    //获取字典表中产线类型
    this.getDicts("zndd_worker_order_status").then(response => {
      this.workOrderStautsS = response.data;
    });
    this.getTimeList();
    this.getPeopleList();
  },
  methods: {
    formatTime(row, column, cellValue) {
      if (cellValue.includes('加时间')) {
        const numberPattern = /\d+/g;
        const numbers = cellValue.match(numberPattern);
        if (numbers && numbers.length > 0) {
          const totalSeconds = parseInt(numbers[0], 10);
          let timeString = '';
          if (totalSeconds >= 3600){
            const hour = Math.floor(totalSeconds / 3600);
            const minutes = Math.floor(totalSeconds / 60) % 60;
            const seconds = Math.floor(totalSeconds % (3600 * hour) % 60);
            timeString = `${hour}小时${minutes}分${seconds}秒`;
          }else if (totalSeconds >= 60){
            const minutes = Math.floor(totalSeconds / 60);
            const seconds = Math.floor(totalSeconds % 60);
            timeString = `${minutes}分${seconds}秒`;
          }else {
            timeString = `${totalSeconds}秒`;
          }
          const beforeTimeString = cellValue.split('加时间')[0];
          const bt = beforeTimeString + '加时间';
          return `${bt}${timeString}`;
        }
      }
      return cellValue;
    },
    //格式化问题类型
    solutionFormatter(row, column) {
      const workOrderStatus = this.workOrderStautsS.find(workOrderStauts => workOrderStauts.dictValue === row.workOrderStauts);
      return workOrderStatus ? workOrderStatus.dictLabel : '';
    },
    /** 序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if (pageNum !== -1 && pageNum !== 0) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return index + 1;
      }
    },
    /** 查询解决方案信息列表 */
    getTimeList() {
      this.loadingTime = true;
      listTimeZndd(this.queryParamsTime).then(response => {
        this.znddListTime = response.rows;
        this.totalTime = response.total;
        this.loadingTime = false;
      });
    },
    /** 查询解决方案信息列表 */
    getPeopleList() {
      this.loadingPeople = true;
      listPeopleZndd(this.queryParamsPeople).then(response => {
        this.znddListPeople = response.rows;
        this.totalPeople = response.total;
        this.loadingPeople = false;
      });
    },
    /** 查询解决方案信息列表 */
    setAttribute(){
      this.queryParamsTime.taskName = this.queryParams.taskName;
      this.queryParamsTime.workOrderName = this.queryParams.workOrderName;
      this.queryParamsTime.workOrderStauts = this.queryParams.workOrderStauts;
      this.queryParamsPeople.taskName = this.queryParams.taskName;
      this.queryParamsPeople.workOrderName = this.queryParams.workOrderName;
      this.queryParamsPeople.workOrderStauts = this.queryParams.workOrderStauts;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskName: null,
        workOrderName: null,
        workOrderNo: null,
        workOrderStauts: null,
        solutionDescribe: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.setAttribute();
      this.getTimeList();
      this.getPeopleList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加解决方案信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getZndd(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改解决方案信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateZndd(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZndd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除解决方案信息编号为"' + ids + '"的数据项？').then(function() {
        return delZndd(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zndd/zndd/export', {
        ...this.queryParams
      }, `zndd_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="css" scoped>

/* 自定义滚动条样式 */
::v-deep ::-webkit-scrollbar {
  width: 12px; /* 水平滚动条的宽度 */
  height: 12px; /* 垂直滚动条的高度 */
  cursor: pointer; /* 鼠标靠近滚动条时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-track {
  background: #f1f1f1; /* 滚动条轨道的颜色 */
  cursor: pointer; /* 鼠标靠近滚动条轨道时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-thumb {
  background: #888; /* 滚动条滑块的颜色 */
  border-radius: 6px; /* 滚动条滑块的圆角 */
  cursor: pointer; /* 鼠标靠近滚动条滑块时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-thumb:hover {
  background: #555; /* 滚动条滑块悬停时的颜色 */
  cursor: pointer; /* 鼠标悬停在滚动条滑块上时仍保持小手样式 */
}
</style>
